package rs.etf.kdp.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ServerSetup extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1167698324206948072L;
	private final JPanel contentPanel = new JPanel();
	private JTextField txtAddress;
	private JTextField txtPort;
	ClientGUI cgui;
	Properties data;

	

	/**
	 * Create the dialog.
	 */
	public ServerSetup(ClientGUI cguiParent) {
		cgui = cguiParent;
		setTitle("Server Setup");
		setBounds(100, 100, 301, 154);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblAddress = new JLabel("Address");
			lblAddress.setBounds(5, 5, 75, 26);
			contentPanel.add(lblAddress);
		}
		{
			txtAddress = new JTextField();
			txtAddress.setBounds(69, 8, 170, 20);
			contentPanel.add(txtAddress);
			txtAddress.setColumns(10);
		}
		{
			JLabel lblPort = new JLabel("Port");
			lblPort.setBounds(5, 41, 57, 26);
			contentPanel.add(lblPort);
		}
		{
			txtPort = new JTextField();
			txtPort.setBounds(69, 44, 170, 20);
			contentPanel.add(txtPort);
			txtPort.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnSave = new JButton("Save");
				btnSave.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						saveProperties();
						cgui.fillInfo();
					}
				});
				buttonPane.add(btnSave);
			}
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						cgui.fillInfo();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
		try {
			loadProperties();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void loadProperties() throws FileNotFoundException, IOException
	{
		data = new Properties();
		data.load(new FileInputStream("res/data.properties"));
		String address, port;
		address = data.getProperty("address");
		port = data.getProperty("port");
		txtAddress.setText(address);
		txtPort.setText(port);
	}
	
	private void saveProperties()
	{
		int port = 0;
		
		try
		{
			port = Integer.parseInt(txtPort.getText());
		} catch(NumberFormatException ex)
		{
			JOptionPane.showMessageDialog(cgui.frmClient, "You have entered invalid value in port text field", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		if(port<1 || port > 65535)
		{
			JOptionPane.showMessageDialog(cgui.frmClient, "You have entered number less than 0 or bigger than 65535 as port", "Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		data.setProperty("address", txtAddress.getText());
		data.setProperty("port", txtPort.getText());
		
		try {
			data.store(new FileOutputStream("res/data.properties"), null);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JOptionPane.showMessageDialog(this, "You have successfully changed server address and port");
		
		
	}

}
